app.service('AuthService', function($http, serverUrl) {
	
	this.login = function(credentials) {
		return $http({
			method : 'POST',
			url : serverUrl + "j_spring_security_check",
			data : $.param({
				'j_username' : credentials.username,
				'j_password' : credentials.password,
				'_spring_security_remember_me' : credentials.remember
			}),
			headers : {
				'Content-Type' : 'application/x-www-form-urlencoded'
			}
		});
	};
	
	this.getCurrentUser = function() {
		return $http({
			method : 'GET',
			url : serverUrl + "services/session/currentUser"
		});
	};
	
	this.logout = function() {
		return $http({
			method : 'GET',
			url : serverUrl + "j_spring_security_logout"
		});
	};
	
});
app.service('UserService', function($http, serverUrl) {
	
	this.getAll = function(pageNo, pageSize) {
		var start = (pageNo - 1) * pageSize;
		return $http({
			method : 'GET',
			url : serverUrl + "services/session/users?start=" + start + "&length=" + pageSize
		});
	};
	
	this.save = function(user) {
		return $http({
			method : 'POST',
			url : serverUrl + "services/session/users",
			data : {
				'username' : user.username,
				'password' : user.password,
				'admin' : user.admin 
			}
		});
	};
	
	this.update = function(user) {
		return $http({
			method : 'PUT',
			url : serverUrl + "services/session/users",
			data : {
				'username' : user.username,
				'password' : user.password,
				'admin' : user.admin 
			}
		});
	};
	
	this.changePassword = function(newPassword) {
		return $http({
			method : 'PUT',
			url : serverUrl + "services/session/users/current/changePassword",
			data : $.param({
				'newPassword' : newPassword
			}),
			headers : {
				'Content-Type' : 'application/x-www-form-urlencoded'
			}
		});
	};
	
	this.enable = function(username) {
		return $http({
			method : 'PUT',
			url : serverUrl + "services/session/users/" + username + "/enable"
		});
	};
	
	this.disable = function(username) {
		return $http({
			method : 'PUT',
			url : serverUrl + "services/session/users/" + username + "/disable"
		});
	};
	
	this.selectedUser = undefined;
	
	this.set = function(user) {
		this.selectedUser = user;
	};
	
	this.get = function() {
		return this.selectedUser;
	};
	
});

app.service('ContactService', function($http, serverUrl) {
	
	this.getAll = function(pageNo) {
		console.log("getAll contacts");
		//FIXME: temp imp
		pageNo = pageNo - 1;
		var data = {};
		data.count = 350;
		data.contacts = [];
		var start = pageNo * 10 + 1;
		var end = start + 50;
		for(var i = start; i < end; i++) {
			var contact = {};
			contact.first = "John";
			contact.last = "Dov " + i;
			contact.phone = 9975709032 + i;
			data.contacts.push(contact);
		}
		return data;
	};
	
	this.save = function(contact) {
		console.log("save contact " + contact);
		//TODO:
	};
	
	this.deleteContact = function(contact) {
		console.log("deleting contact " + contact);
		//TODO:
	};
	
	this.selectedContact = {};
	
	this.set = function(contact) {
		console.log("set contact " + contact);
		this.selectedContact = contact;
	};
	
	this.get = function() {
		console.log("get contact " + this.selectedContact);
		return this.selectedContact;
	};
	
});